local skel = fk.CreateSkill {
  name = "emo__caiyong",
}

Fk:loadTranslationTable{
  ["emo__caiyong"] = "才涌",
  [":emo__caiyong"] = "一名角色出牌阶段开始时，你可以为选择一个未被完成的任务，若其于此阶段内完成了任务，其于阶段结束时获得相应奖励：<br>"..
  "①仅使用了一张牌，奖励：回复1点体力。<br>"..
  "②仅指定二名角色成为目标，奖励：摸两张牌。<br>"..
  "③使用了三种类型的牌，奖励：卜算3并摸三张牌。<br>"..
  "④使用了四种花色的牌，奖励：摸四张牌且并分配四张手牌。",

  ["#emo__caiyong_delay"] = "才涌",
  ["@[:]emo__caiyong-phase"] = "才涌",
  ["#emo__caiyong-invoke"] = "才涌：是否为 %src 指定一个任务，其完成后可获得奖励",
  ["#emo__caiyong-choice"] = "才涌：选择一个任务，其完成后可获得奖励",
  ["#emo__caiyong-yiji"] = "才涌：你可以分配至多4张手牌（可以给自己）",
  ["#emo__caiyong_success"] = "%from 完成任务 %arg",

  ["caiyong_times"] = "牌数一",
  ["caiyong_targets"] = "目标二",
  ["caiyong_types"] = "类型三",
  ["caiyong_suits"] = "花色四",
  [":caiyong_times"] = "仅使用了一张牌，奖励：回复1点体力",
  [":caiyong_targets"] = "仅指定二名角色成为目标，奖励：摸两张牌",
  [":caiyong_types"] = "使用了三种类型的牌，奖励：卜算3并摸三张牌",
  [":caiyong_suits"] = "使用了四种花色的牌，奖励：摸四张牌且并分配四张手牌",
}

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if target.phase == Player.Play and player:hasSkill(skel.name) and not target.dead then
      local mark = player.room:getBanner("emo__caiyong_completed") or Util.DummyTable
      return #mark < 4
    end
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#emo__caiyong-invoke:"..target.id }) then
      event:setCostData(self, { tos = { target } } )
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local all_choices = {"caiyong_times", "caiyong_targets", "caiyong_types", "caiyong_suits"}
    local completed = player.room:getBanner("emo__caiyong_completed") or Util.DummyTable
    local choices = table.filter(all_choices, function(choice) return not table.contains(completed, choice) end)
    local choice = room:askToChoice(player, {
      choices = choices, all_choices = all_choices, detailed = true, prompt = "#emo__caiyong-choice", skill_name = skel.name,
    })
    room:addTableMark(target, "@[:]emo__caiyong-phase", choice)
  end,
})

skel:addEffect(fk.EventPhaseEnd, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target:getMark("@[:]emo__caiyong-phase") ~= 0 and not target.dead and target.phase == Player.Play
    and target == player
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local times = 0
    local targets, types, suits = {}, {}, {}
    player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
      local use = e.data
      if use and use.from == target then
        times = times + 1
        for _, to in ipairs(use.tos) do
          table.insertIfNeed(targets, to)
        end
        table.insertIfNeed(types, use.card.type)
        if use.card.suit ~= Card.NoSuit then
          table.insertIfNeed(suits, use.card.suit)
        end
      end return false
    end, Player.HistoryPhase)
    local record = target:getMark("@[:]emo__caiyong-phase")
    local completed = room:getBanner("emo__caiyong_completed") or {}
    local new = {}
    if table.contains(record, "caiyong_times") and times == 1 then
      table.insert(new, "caiyong_times")
    end
    if table.contains(record, "caiyong_targets") and #targets == 2 then
      table.insert(new, "caiyong_targets")
    end
    if table.contains(record, "caiyong_types") and #types == 3 then
      table.insert(new, "caiyong_types")
    end
    if table.contains(record, "caiyong_suits") and #suits == 4 then
      table.insert(new, "caiyong_suits")
    end
    if #new == 0 then return end
    table.insertTable(completed, new)
    room:setBanner("emo__caiyong_completed", completed)
    for _, s in ipairs(new) do
      room:sendLog{ type = "#emo__caiyong_success", from = target.id, toast = true, arg = ":" .. s }
    end

    local me = table.find(room.alive_players, function(p) return p:hasSkill(skel.name, true) end)
    if me then
      room:notifySkillInvoked(me, "emo__caiyong", "support", {target.id})
      room:doIndicate(me, {target})
    end
    if table.contains(new, "caiyong_times") then
      if target:isWounded() then
        room:recover { num = 1, skillName = skel.name, who = target , recoverBy = me or target }
      end
    end
    if table.contains(new, "caiyong_targets") and target:isAlive() then
      target:drawCards(2, skel.name)
    end
    if table.contains(new, "caiyong_types") and target:isAlive() then
      room:askToGuanxing(player, {
        cards = room:getNCards(3), skill_name = skel.name, skip = false,
      })
      if not target.dead then
        target:drawCards(3, skel.name)
      end
    end
    if table.contains(new, "caiyong_suits") and target:isAlive() then
      target:drawCards(4, skel.name)
      if not target.dead and not target:isKongcheng() then
        room:askToYiji(target, {
          cards = target:getCardIds("h"), min_num = 0, max_num = 4, skill_name = skel.name, cancelable = true,
          prompt = "#emo__caiyong-yiji", targets = room:getAlivePlayers()
        })
      end
    end
  end,
})

return skel
